//
//  WSLoginView.m
//  FamilyLinkPro
//
//  Created by 江鹏 on 2023/9/7.
//  Copyright © 2023 SiterWell.Co.,Ltd. All rights reserved.
//

#import "WSLoginView.h"

@implementation WSLoginView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubviews];
    }
    return self;
}

// MARK: 添加子控件
- (void)setupSubviews {
    UILabel *loginLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    loginLabel.text = @"登录";
    [self addSubview:loginLabel];
    loginLabel.textColor = UIColorFromRGB_0x(0x000000);
    loginLabel.font = fontBold(24);
    [loginLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(20);
        make.top.mas_equalTo(self).offset(NaviHeight + 22);
    }];
    
    UIView *textAreaView = [[UIView alloc] initWithFrame:CGRectZero];
    textAreaView.backgroundColor = UIColorFromRGB_0x(0xF2F5F8);
    [self addSubview:textAreaView];
    [textAreaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(37);
        make.right.mas_equalTo(self).offset(-37);
        make.top.mas_equalTo(loginLabel.mas_bottom).offset(45);
        make.height.mas_equalTo(122);
    }];
    ViewRadius(textAreaView, 10);
    
    _accountTF = [[UITextField alloc] initWithFrame:CGRectZero];
    UITextField *accountTF = _accountTF;
    [textAreaView addSubview:accountTF];
    accountTF.backgroundColor = ClearColor;
    [accountTF mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.mas_equalTo(self);
        make.left.mas_equalTo(textAreaView).offset(26);
        make.right.mas_equalTo(textAreaView).offset(-30);
        make.height.mas_equalTo(45);
        make.top.mas_equalTo(textAreaView).offset(10).priorityLow();
    }];
    accountTF.keyboardType = UIKeyboardTypeEmailAddress;
    accountTF.borderStyle = UITextBorderStyleNone;
    accountTF.textAlignment = NSTextAlignmentNatural;
    accountTF.font = font(16);
    accountTF.textColor = BlackColor;
    accountTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    accountTF.leftViewMode = UITextFieldViewModeAlways;
    [accountTF addTarget:self action:@selector(accountTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
 
    
//    ViewRadius(emailTextField, 10);
    accountTF.attributedPlaceholder = [accountTF placeHolderAttributedString:@"请输入账号:" color:UIColorFromRGB_0x(0x666666) fontSize:16];
    accountTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectZero];
    [textAreaView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(accountTF);
        make.right.mas_equalTo(accountTF);
        make.top.mas_equalTo(accountTF.mas_bottom).offset(5);
        make.height.mas_equalTo(1);
    }];
    lineView.backgroundColor = UIColorFromRGB_0x(0xDDDDDD);
//    lineView.backgroundColor = RedColor;
    
    _passwordTF = [[UITextField alloc] initWithFrame:CGRectZero];
    UITextField *passwordTF = _passwordTF;
    [textAreaView addSubview:passwordTF];
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(accountTF);
        make.height.mas_equalTo(accountTF);
        make.top.mas_equalTo(lineView.mas_bottom).offset(10).priorityLow();
    }];
    
    passwordTF.keyboardType = UIKeyboardTypeEmailAddress;
    passwordTF.borderStyle = UITextBorderStyleNone;
    passwordTF.textAlignment = NSTextAlignmentNatural;
    passwordTF.textColor = BlackColor;
    passwordTF.font = font(16);
    passwordTF.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 0)];
    passwordTF.leftViewMode = UITextFieldViewModeAlways;
    [passwordTF addTarget:self action:@selector(passwordTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
 
    
//    ViewRadius(emailTextField, 10);
    passwordTF.attributedPlaceholder = [accountTF placeHolderAttributedString:@"请输入密码:" color:UIColorFromRGB_0x(0x666666) fontSize:16];
    passwordTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordTF.secureTextEntry = YES;
    
    _securityButton = [UIButton buttonWithType:UIButtonTypeCustom];
    UIButton *securityButton = _securityButton;
    [passwordTF addSubview:securityButton];
    [securityButton setImage:ImageNamed(@"icon_eye_close") forState:UIControlStateNormal];
    [securityButton setImage:ImageNamed(@"icon_eye_open") forState:UIControlStateSelected];
    [securityButton addTarget:self action:@selector(securityButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [securityButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(passwordTF);
        make.right.mas_equalTo(passwordTF).offset(-30);
    }];
    securityButton.hidden = YES;
    
    _agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_agreeButton];
    [_agreeButton setBackgroundImage:ImageNamed(@"unselected_48") forState:UIControlStateNormal];
    [_agreeButton setBackgroundImage:ImageNamed(@"selected_48") forState:UIControlStateSelected];
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(textAreaView);
        make.top.mas_equalTo(textAreaView.mas_bottom).offset(33);
    }];
    [_agreeButton addTarget:self action:@selector(agreeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *agreeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    agreeLabel.text = @"我同意";
    agreeLabel.textColor = UIColorFromRGB_0x(0x666666);
    agreeLabel.font = font(13);
    [self addSubview:agreeLabel];
    [agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_agreeButton.mas_right).offset(15);
        make.centerY.mas_equalTo(_agreeButton);
    }];
    
    UILabel *userProtocolLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    userProtocolLabel.textColor = UIColorFromRGB_0x(0x4C80F8);
    userProtocolLabel.text = @"用户协议";
    userProtocolLabel.font = font(13);
    [self addSubview:userProtocolLabel];
    userProtocolLabel.userInteractionEnabled = YES;
    [userProtocolLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(agreeLabel.mas_right).offset(0);
    }];
    
    UITapGestureRecognizer *userProtocolRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(userProtocolClick)];
    [userProtocolLabel addGestureRecognizer:userProtocolRecognizer];
    
    
    UILabel *andLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:andLabel];
    andLabel.text = @"和";
    andLabel.font = font(13);
    andLabel.textColor = UIColorFromRGB_0x(0x666666);
    [andLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(userProtocolLabel.mas_right).offset(0);
    }];
    
    UILabel *privacyLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [self addSubview:privacyLabel];
    privacyLabel.text = @"隐私政策";
    privacyLabel.font = font(13);
    privacyLabel.textColor = UIColorFromRGB_0x(0x4C80F8);
    privacyLabel.userInteractionEnabled = YES;
    [privacyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(agreeLabel);
        make.left.mas_equalTo(andLabel.mas_right).offset(0);
    }];
    UITapGestureRecognizer *privacyRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(privacyClick)];
    [privacyLabel addGestureRecognizer:privacyRecognizer];
    
    
    _loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_loginButton];
    [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
    _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
    _loginButton.titleLabel.font = fontBold(16);
    ViewRadius(_loginButton, 20);
    [_loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_agreeButton);
        make.right.mas_equalTo(textAreaView);
        make.top.mas_equalTo(privacyLabel.mas_bottom).offset(30);
        make.height.mas_equalTo(45);
    }];
    _loginButton.userInteractionEnabled = YES;//正式userInteractionEnabled 改为NO
    [_loginButton addTarget:self action:@selector(loginButtonClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *forgetPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:forgetPasswordButton];
    [forgetPasswordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(_loginButton.mas_bottom).offset(25);
    }];
    [forgetPasswordButton setTitle:@"忘记密码" forState:UIControlStateNormal];
    forgetPasswordButton.titleLabel.font = font(14);
    [forgetPasswordButton setTitleColor:UIColorFromRGB_0x(0x4C80F8) forState:UIControlStateNormal];
    [forgetPasswordButton addTarget:self action:@selector(forgetPasswordButtonClick) forControlEvents:UIControlEventTouchUpInside];
}


// MARK: action

- (void)accountTextFieldChange:(UITextField *)sender { //正式用的请把注释打开
//    BOOL isAccountBlankString = [NSString isBlankString:sender.text];
//    BOOL isPasswordBlankString = [NSString isBlankString:_passwordTF.text];
//    if (isAccountBlankString || isPasswordBlankString || !_agreeButton.isSelected) {
//        _loginButton.userInteractionEnabled = NO;
//        _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
//        return;
//    }
//    _loginButton.userInteractionEnabled = YES;
//    _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 1);
    
    
}

- (void)passwordTextFieldChange:(UITextField *)sender { //正式用的请把注释打开
//    BOOL isPasswordBlankString = [NSString isBlankString:sender.text];
//    BOOL isAccountBlankString = [NSString isBlankString:_accountTF.text];
//    _securityButton.hidden = isAccountBlankString;
//    if (isAccountBlankString || isPasswordBlankString || !_agreeButton.isSelected) {
//        _loginButton.userInteractionEnabled = NO;
//        _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
//        return;
//    }
//    _loginButton.userInteractionEnabled = YES;
//    _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 1);
    
    
}

- (void)securityButtonClick:(UIButton *)sender { //正式用的请把注释打开
//    BOOL flag = sender.selected;
//    [sender setSelected:!flag];
//    _passwordTF.secureTextEntry = flag;
}

- (void)agreeButtonClick:(UIButton *)sender { //正式用的请把注释打开
//    sender.selected = !sender.selected;
//    BOOL isAccountBlankString = [NSString isBlankString:_accountTF.text];
//    BOOL isPasswordBlankString = [NSString isBlankString:_passwordTF.text];
//    if (isAccountBlankString || isPasswordBlankString || !sender.isSelected) {
//        _loginButton.userInteractionEnabled = NO;
//        _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 0.6);
//        return;
//    }
//    _loginButton.userInteractionEnabled = YES;
//    _loginButton.backgroundColor = UIColorFromAlphaRGB_0x(0x4C80F8, 1);
    
    
    
}

- (void)userProtocolClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userProtocolClick)]) {
        [self.delegate userProtocolClick];
    }
}

- (void)privacyClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(privacyClick)]) {
        [self.delegate privacyClick];
    }
}

- (void)loginButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(loginButtonClickWithAccount:withPassword:)]) {
        [self.delegate loginButtonClickWithAccount:_accountTF.text withPassword:_passwordTF.text];
    }
}


- (void)forgetPasswordButtonClick {
    if (self.delegate && [self.delegate respondsToSelector:@selector(forgetPasswordButtonClick)]) {
        [self.delegate forgetPasswordButtonClick];
    }
}

@end
